//
//  GYSCircleSkipButton.m
//  AiHome
//
//  Created by macbook on 2018/1/27.
//  Copyright © 2018年 yguan. All rights reserved.
//

#import "GYSCircleSkipButton.h"

//度数转角度
#define degreeToAngle(x) ((x) * M_PI / 180.0)

@interface GYSCircleSkipButton ()

// 底部进度条圈
@property (nonatomic, strong) CAShapeLayer *circleLayer;
// 表层进度条圈
@property (nonatomic, strong) CAShapeLayer *progressLayer;
@property (nonatomic, strong) UIBezierPath *bezierPath;
@property (nonatomic, copy)   CircleSkipButtonBlock myBlock;

@end


@implementation GYSCircleSkipButton

- (instancetype) initWithFrame:(CGRect)frame {
    if(self == [super initWithFrame:frame]){
        self.backgroundColor = [UIColor clearColor];
        [self.layer addSublayer:self.circleLayer];
    }
    return self;
}

- (UIBezierPath *)bezierPath  {
    if(!_bezierPath) {
        //获取半径
        const CGFloat radius = CGRectGetWidth(self.frame) / 2.0f;
        CGFloat width = radius;
        CGFloat height = radius;
        CGPoint centerPoint = CGPointMake(width, height);
        _bezierPath = [UIBezierPath bezierPathWithArcCenter:centerPoint radius:radius startAngle:degreeToAngle(-90) endAngle:degreeToAngle(270) clockwise:YES];
    }
    return _bezierPath;
}

- (CAShapeLayer *)circleLayer {
    if (!_circleLayer) {
        _circleLayer = [CAShapeLayer layer];
        _circleLayer.frame = self.bounds;
        // 圈内填充色
        _circleLayer.fillColor = self.fillColor.CGColor ? self.fillColor.CGColor : [UIColor clearColor].CGColor;
        _circleLayer.lineWidth = self.lineWidth ? self.lineWidth : 2.0f;
        // 底部圈色
        _circleLayer.strokeColor = self.circleColor.CGColor ? self.circleColor.CGColor : [UIColor colorWithRed:197/255.0 green:159/255.0 blue:82/255.0 alpha:0.3].CGColor ;
        _circleLayer.strokeStart = 0.0f;
        _circleLayer.strokeEnd = 1.0f;
        
        _circleLayer.path = self.bezierPath.CGPath;
    }
    return _circleLayer;
}

- (CAShapeLayer *)progressLayer {
    
    if (!_progressLayer) {
        _progressLayer = [CAShapeLayer layer];
        _progressLayer.frame = self.bounds;
        _progressLayer.fillColor = [UIColor clearColor].CGColor;
        _progressLayer.lineWidth = self.lineWidth ? self.lineWidth : 2.f;
        _progressLayer.lineCap = kCALineCapRound;
        // 进度条圈进度色
        _progressLayer.strokeColor = self.progressColor.CGColor ? self.progressColor.CGColor  : [UIColor  colorWithRed:197/255.0 green:159/255.0 blue:82/255.0 alpha:1].CGColor;
        _progressLayer.strokeStart = 0.f;
        
        CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
        pathAnimation.duration = self.animationDuration;
        pathAnimation.fromValue = @(0.0);
        pathAnimation.toValue = @(1.0);
        pathAnimation.removedOnCompletion = YES;
        pathAnimation.delegate = self;
        [_progressLayer addAnimation:pathAnimation forKey:nil];
        
        _progressLayer.path = _bezierPath.CGPath;
    }
    return _progressLayer;
}

#pragma mark -- CAAnimationDelegate
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    if (flag) {
        self.myBlock();
    }
}

#pragma mark ---
- (void)startAnimationDuration:(CGFloat)duration withBlock:(CircleSkipButtonBlock )block {
    self.myBlock = block;
    self.animationDuration = duration;
    [self.layer addSublayer:self.progressLayer];
}

@end
